﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace SqlServer
{
    public partial class Form1 : Form
    {

        public static string connectionString = "Data Source=localhost;Initial Catalog=C#sql;User ID=C#test;Password=123";
        
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();

                    // 打开表1
                    DataTable dataTable1 = new DataTable();
                    SqlDataAdapter dataAdapter1 = new SqlDataAdapter("SELECT * FROM 学生", connection);
                    dataAdapter1.Fill(dataTable1);
                    dataGridView1.DataSource = dataTable1;

                    // 打开表2
                    DataTable dataTable2 = new DataTable();
                    SqlDataAdapter dataAdapter2 = new SqlDataAdapter("SELECT * FROM 老师", connection);
                    dataAdapter2.Fill(dataTable2);
                    dataGridView2.DataSource = dataTable2;

                    // 打开表3
                    DataTable dataTable3 = new DataTable();
                    SqlDataAdapter dataAdapter3 = new SqlDataAdapter("SELECT * FROM 其他员工", connection);
                    dataAdapter3.Fill(dataTable3);
                    dataGridView3.DataSource = dataTable3;

                    MessageBox.Show("数据库连接成功！");
                    connection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库连接失败： " + ex.Message);
                }
            }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string dataname = tabControl1.SelectedTab.Text;
            if (dataname == tabPage1.Text)
            {
                Form2 form2 = new Form2(this);
                form2.Show();
            }
            if (dataname == tabPage2.Text || dataname == tabPage3.Text)
            {
                Form4 form4 = new Form4(this,dataname);
                form4.Show();
            }
        }

        public void RefreshData()
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();

                    // 刷新表1
                    DataTable dataTable1 = new DataTable();
                    SqlDataAdapter dataAdapter1 = new SqlDataAdapter("SELECT * FROM 学生", connection);
                    dataAdapter1.Fill(dataTable1);
                    dataGridView1.DataSource = dataTable1;

                    // 刷新表2
                    DataTable dataTable2 = new DataTable();
                    SqlDataAdapter dataAdapter2 = new SqlDataAdapter("SELECT * FROM 老师", connection);
                    dataAdapter2.Fill(dataTable2);
                    dataGridView2.DataSource = dataTable2;

                    // 刷新表3
                    DataTable dataTable3 = new DataTable();
                    SqlDataAdapter dataAdapter3 = new SqlDataAdapter("SELECT * FROM 其他员工", connection);
                    dataAdapter3.Fill(dataTable3);
                    dataGridView3.DataSource = dataTable3;

                    connection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库刷新失败： " + ex.Message);
                }
            }
        }
    }
}
